package com.breakfast.fun.view.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.breakfast.fun.R;


/**
 * <p>
 * Title: CustomDialog
 * </p>
 * <p>
 * Description:自定义Dialog（参数传入Dialog样式文件，Dialog布局文件）
 * </p>
 * <p>
 * Copyright: Copyright (c) 2014
 * </p>
 *
 * @author zihao
 * @version 1.01
 */
public class CustomDialog extends Dialog implements android.view.View.OnClickListener {

    /**
     * 布局文件
     **/
    int layoutRes;
    /**
     * 上下文对象
     **/
    Context context;
    /**
     * 标题
     **/
    private TextView title;
    /**
     * 提示信息
     **/
    private TextView tip;
    /**
     * 确定按钮
     **/
    private Button confirmBtn;
    /**
     * 取消按钮
     **/
    private Button cancelBtn;
    /**
     * Toast时间
     **/
    public static final int TOAST_TIME = 1000;

    private boolean isCanCancle;

    private boolean isPermission;
    private View divier;

    String mtitle;
    String mtip;
    android.view.View.OnClickListener listenner;

    public CustomDialog(Context context) {
        super(context);
        this.context = context;
    }

    /**
     * 自定义布局的构造方法
     *
     * @param context
     * @param resLayout
     */
    public CustomDialog(Context context, int resLayout) {
        super(context);
        this.context = context;
        this.layoutRes = resLayout;
    }

    /**
     * 自定义主题及布局的构造方法
     *
     * @param context
     * @param theme
     * @param resLayout
     * @param mtitle    标题
     * @param mtip      提示
     */
    public CustomDialog(Context context, int theme, int resLayout, String mtitle, String mtip, boolean isCanCancle, boolean isPermission,
                        android.view.View.OnClickListener listenner) {
        super(context, theme);
        this.context = context;
        this.layoutRes = resLayout;
        this.mtitle = mtitle;
        this.mtip = mtip;
        this.isCanCancle = isCanCancle;
        this.isPermission = isPermission;
        this.listenner = listenner;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定布局
        this.setContentView(layoutRes);
        initView(mtitle, mtip);
    }

    private void initView(String mtitle, String mtip) {
        // 根据id在布局中找到控件对象
        title = (TextView) findViewById(R.id.dialog_title);
        tip = (TextView) findViewById(R.id.dialog_tip);
        confirmBtn = (Button) findViewById(R.id.confirm_btn);
        cancelBtn = (Button) findViewById(R.id.cancel_btn);
        divier = (View) findViewById(R.id.divier);

        // 设置按钮的文本颜色
        confirmBtn.setTextColor(0xff1E90FF);
        cancelBtn.setTextColor(0xff1E90FF);

        title.setText(mtitle);
        tip.setText(mtip);

        // // 为按钮绑定点击事件监听器
        confirmBtn.setOnClickListener(listenner);
        if (isCanCancle) {//不可取消
            cancelBtn.setOnClickListener(null);
            cancelBtn.setVisibility(View.GONE);
            divier.setVisibility(View.GONE);
            this.setCanceledOnTouchOutside(false);

        } else {
            cancelBtn.setOnClickListener(this);
            cancelBtn.setVisibility(View.VISIBLE);
            divier.setVisibility(View.VISIBLE);
            this.setCanceledOnTouchOutside(true);
        }

        if (isPermission) {
            cancelBtn.setOnClickListener(v -> {
                ((Activity) context).finish();
            });
            cancelBtn.setText("拒绝");
            cancelBtn.setVisibility(View.VISIBLE);
            divier.setVisibility(View.VISIBLE);
            this.setCanceledOnTouchOutside(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.cancel_btn:
                this.dismiss();
                break;

            default:
                break;
        }
    }
}